// 区分开发环境和生产环境  接口地址


export const baseUrl = 'https://lawyer.yic3.cn/'; //正式地址
// export const baseUrl = 'https://lawyer.orgpeel.cn/'; //测试地址
// export const baseUrl = 'http://192.168.31.102:9303/'; //测试环境
import store from '@/store/index.js'; //需要引入store

export function https(config) {
	//设置请求头
	config.header = {
		'content-type': 'application/json;charset=utf-8',
		'x-app-id': '10026',
		'x-token': uni.getStorageSync('Token') || '',
		// #ifdef MP
		'x-platform': 'wxapplet',
		'x-version': '2.1.4',
		// #endif
		// #ifdef APP || APP-PLUS
		'x-platform': 'ios',
		'x-version': uni.getStorageSync("saveAllList").appVersion,
		// #endif
		'x-device-id': uni.getStorageSync("saveAllList").deviceId,
		'x-channel': uni.getStorageSync('channel'),
		'x-mobile-brand': uni.getStorageSync("saveAllList").brand || "",
		'x-mobile-model': uni.getStorageSync("saveAllList").model,
		'x-user-agent': '4051322344813113401',
		// 设置响应头信息
		
	};
	
	config.url = baseUrl + config.url; // 请求地址
	let promise = new Promise(function(resolve, reject) {
		uni.request(config).then(res => {
			if (res.data.code == 1004) {
				uni.setStorageSync('Token', null)
				uni.showToast({
					title: '身份已过期，跳转至首页',
					icon: 'none'
				})
				return setTimeout(() => {
					uni.reLaunch({
						// url: '/pages/index/home/home'
						url:  '/upload/pages/chooseFiles/chooseFiles'
					})
				}, 1000)
			}
			if (res[0]) {
				uni.showToast({
					title: "数据获取失败",
					icon: "none",
					duration: 1500
				})
				resolve(false);
			} else {
				resolve(res);
			}
		}).catch(error => {
			console.log(error)
			reject(error);
		})
	})
	return promise
}
export const uploadFilePromise = function(url) {
	return new Promise((resolve, reject) => {
		let a = uni.uploadFile({
			url: baseUrl + 'api/upload', // 仅为示例，非真实的接口地址
			filePath: url,
			name: 'file',
			header: {
				'content-type': 'application/form-data',
				'x-app-id': '10028',
				'x-token': uni.getStorageSync('Token'),
				'x-version': uni.getStorageSync("saveAllList").appVersion,
				'x-platform': uni.getStorageSync("saveAllList").osName,
				'x-device-id': uni.getStorageSync("saveAllList").deviceId,
				'x-channel': 'ocean',
				'x-mobile-brand': uni.getStorageSync("saveAllList").brand || "",
				'x-mobile-model': uni.getStorageSync("saveAllList").model,
				'x-user-agent': '4051322344813113401',
			},
			success: (res) => {
				console.log(res);
				resolve(JSON.parse(res.data).data)
			},
			fail(err) {
				console.log("请求失败的结果", res);
				reject(res);
			}
		});
	})
}